package com.qfedu.edu.slot.impl;

import com.qfedu.edu.exception.handler.BusinessException;
import com.qfedu.edu.result.ResponseCode;
import com.qfedu.edu.slot.base.SlotChain;
import com.qfedu.edu.slot.generate.GenerateSlot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

/**
 * @author xiaobobo
 * @title: SlotChainImpl
 * @projectName cd-fy-2401-third-project-parent
 * @description: TODO
 * @date 2024/9/23  15:39
 */
@Component
public class SlotChainImpl implements SlotChain {

    @Autowired
    private List<GenerateSlot> slotList;

    //初始化一个容器 没得人来该他
    private TreeSet<GenerateSlot> generateSlots = new TreeSet<>();

    @PostConstruct
    public void init() {
        for (int i = 0; i < slotList.size(); i++) {
            generateSlots.add(slotList.get(i));
        }
    }

    @Override
    public boolean dealReq(HttpServletRequest req) {
        Iterator<GenerateSlot> iterator = generateSlots.iterator();
        while (iterator.hasNext()) {
            GenerateSlot next = iterator.next();
            if (!next.dealReq(req)) {
                //说明不能放行
                throw new BusinessException(ResponseCode.SLOT_INVOKE_EXCEPTION.getCode(), ResponseCode.SLOT_INVOKE_EXCEPTION.getMsg());
            }
        }
        return true;
    }
}
